package com.whz.workmanageol.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whz.workmanageol.dao.CourseMapper;
import com.whz.workmanageol.dao.GradeMapper;
import com.whz.workmanageol.dao.StudentMapper;
import com.whz.workmanageol.dao.TeacherMapper;
import com.whz.workmanageol.entity.Course;
import com.whz.workmanageol.entity.Grade;
import com.whz.workmanageol.entity.Student;
import com.whz.workmanageol.entity.Teacher;
import com.whz.workmanageol.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class AdminServiceImpl implements AdminService {
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private GradeMapper gradeMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public PageInfo<Teacher> teacherList(Integer page) {
        PageHelper.startPage(page, 10);
        List<Teacher> teachers = teacherMapper.selectAll();
        return new PageInfo<>(teachers);
    }

    @Override
    public PageInfo<Student> studentList(Integer page) {
        PageHelper.startPage(page, 10);
        List<Student> students = studentMapper.selectAll();
        return new PageInfo<>(students);
    }

    @Override
    public PageInfo<Grade> gradeList(Integer page) {
        PageHelper.startPage(page, 10);
        List<Grade> grades = gradeMapper.selectAll();
        return new PageInfo<>(grades);
    }

    @Override
    public PageInfo<Course> courseList(Integer page) {
        PageHelper.startPage(page, 10);
        List<Course> courses = courseMapper.selectAll();
        return new PageInfo<>(courses);
    }

    @Override
    public void updateTeacherInfo(Teacher teacher) {
        Teacher theTea = teacherMapper.selectByPrimaryKey(teacher.getId());
        theTea.setTeacherid(teacher.getTeacherid());
        theTea.setUsername(teacher.getUsername());
        theTea.setTel(teacher.getTel());
        theTea.setEmail(teacher.getEmail());
        teacherMapper.updateByPrimaryKeySelective(theTea);
    }

    @Override
    public void updateStudentInfo(Student student) {
        Student theStu = studentMapper.selectByPrimaryKey(student.getId());
        theStu.setStudentid(student.getStudentid());
        theStu.setUsername(student.getUsername());
        theStu.setEmail(student.getEmail());
        theStu.setTel(student.getTel());
        studentMapper.updateByPrimaryKeySelective(theStu);
    }

    @Override
    public void updateGradeInfo(Grade grade) {
        Grade theGra = gradeMapper.selectByPrimaryKey(grade.getId());
        theGra.setGradeid(grade.getGradeid());
        theGra.setGradename(grade.getGradename());
        gradeMapper.updateByPrimaryKeySelective(theGra);
    }

    @Override
    public void updateCourseInfo(Course course) {
        Course theCou = courseMapper.selectByPrimaryKey(course.getId());
        theCou.setCourseid(course.getCourseid());
        theCou.setCoursename(course.getCoursename());
        courseMapper.updateByPrimaryKeySelective(theCou);
    }

    @Override
    public void delTeacherInfo(String teacherid) {
        teacherMapper.deleteByPrimaryKey(teacherid);
    }

    @Override
    public void delStudentInfo(String studentid) {
        studentMapper.deleteByPrimaryKey(studentid);
    }

    @Override
    public void delGradeInfo(String gradeid) {
        gradeMapper.deleteByPrimaryKey(gradeid);
    }

    @Override
    public void delCourseInfo(String courseid) {
        courseMapper.deleteByPrimaryKey(courseid);
    }

    @Override
    public List<Teacher> searchTeacher(String teacherid) {
        Example example = new Example(Teacher.class);
        example.createCriteria().andEqualTo("teacherid", teacherid);
        return teacherMapper.selectByExample(example);
    }

    @Override
    public List<Student> searchStudent(String studentid) {
        Example example = new Example(Student.class);
        example.createCriteria().andEqualTo("studentid", studentid);
        return studentMapper.selectByExample(example);
    }

    @Override
    public List<Grade> searchGrade(String gradename) {
        Example example = new Example(Grade.class);
        example.createCriteria().andEqualTo("gradename", gradename);
        return gradeMapper.selectByExample(example);
    }

    @Override
    public List<Course> searchCourse(String coursename) {
        Example example = new Example(Course.class);
        example.createCriteria().andEqualTo("coursename", coursename);
        return courseMapper.selectByExample(example);
    }
}
